package com.xbongbong.paas.help.optional.range.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.help.optional.range.UserDeptOptionalRangeHandler;
import com.xbongbong.paas.pojo.dto.UserInfoDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.enums.businessrule.enums.RuleValueTypeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.user.pojo.ReturnUserAndDepartmentPojo;
import com.xbongbong.saas.domain.entity.ProductUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.business.ProductEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author sunshaoxiang
 * @Description 部门名称条件过滤
 * @date 2021/9/9 10:11
 * @Version 1.0
 */
public class DeptNameHandlerImpl implements UserDeptOptionalRangeHandler {
    private static final Logger LOG = LoggerFactory.getLogger(DeptNameHandlerImpl.class);

    @Override
    public Map<String, Object> filterDeptUserData(FilterConditionsPoJo filterConditionsPoJo, JSONObject optionalRangeDataList, PaasFormDataEntity paasFormDataEntity, Map<Integer, List<UserTeamEntity>> userTeam, List<ProductUserEntity> productUserList, UserInfoDTO loginUserInfo) throws XbbException {
        List<Long> deptIdIn = null;
        List<Long> deptIdNoIn = null;
        HashMap<String, Object> deptMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        //获取当前登录用户信息
        UserVO loginUser = loginUserInfo.getLoginUser();
        Integer valueType = filterConditionsPoJo.getValueType();
        //判断过滤条件类型
        String symbol = filterConditionsPoJo.getSymbol();
        boolean conditionSymbol = true;
        RuleValueTypeEnum ruleValueTypeEnum = RuleValueTypeEnum.getByType(valueType);
        try {
            if (Objects.equals(ConditionEnum.NOIN.getSymbol(), symbol)){
                conditionSymbol = false;
                deptIdNoIn = new ArrayList<>();
            }else {
                deptIdIn = new ArrayList<>();
            }
            switch (ruleValueTypeEnum) {
                //处理动态值
                case DYNAMIC_VALUE:
                    //动态值字段为当前登录部门
                    if (Objects.equals(filterConditionsPoJo.getValueFieldType(), FieldTypeEnum.REGISTER_DEPT.getType())) {
                        String department = loginUser.getDepartment();
                        List<Long> deptIds = JSONArray.parseArray(department, Long.class);
                        if (CollectionsUtil.isNotEmpty(deptIds)){
                            for (Long deptId : deptIds) {
                                if (conditionSymbol) {
                                    deptIdIn.add(deptId);
                                } else{
                                    deptIdNoIn.add(deptId);
                                }
                            }
                        }
                    } else {
                        String dynamicAttr = filterConditionsPoJo.getValueAttr();
                        if (Objects.equals(filterConditionsPoJo.getValueFieldType(), FieldTypeEnum.DEPT_GROUP.getType())) {
                            if (Objects.nonNull(optionalRangeDataList)){
                                List<LinkedHashMap> departmentList = (List) optionalRangeDataList.get(dynamicAttr);
                                if (CollectionsUtil.isNotEmpty(departmentList)){
                                    for (LinkedHashMap dept : departmentList) {
                                        Long deptId = Long.valueOf(String.valueOf(dept.get("id")));
                                        if (conditionSymbol) {
                                            deptIdIn.add(deptId);
                                        } else{
                                            deptIdNoIn.add(deptId);
                                        }
                                    }
                                }
                            }else {
                                List<Long> deptIds = JSONArray.parseArray(JSON.toJSONString(paasFormDataEntity.getData().get(dynamicAttr)), Long.class);
                                if (CollectionsUtil.isNotEmpty(deptIds)){
                                    for (Long deptId : deptIds) {
                                        if (conditionSymbol) {
                                            deptIdIn.add(deptId);
                                        } else{
                                            deptIdNoIn.add(deptId);
                                        }
                                    }
                                //特殊情况 后端拿可见部门的数据
                                    // todo-leex 服务项目
                                }else if (Objects.equals(dynamicAttr, ProductEnum.VISBLE_DEPT.getAttr())){
                                    for (ProductUserEntity productUser : productUserList) {
                                        if (Objects.equals(productUser.getType(), 1)){
                                            Long deptId = StringUtil.StringToLong(productUser.getRelationId());
                                            if (Objects.equals(deptId, 0L)) {
                                                deptId = 1L;
                                            }
                                            if (conditionSymbol) {
                                                deptIdIn.add(deptId);
                                            } else{
                                                deptIdNoIn.add(deptId);
                                            }
                                        }
                                    }
                                }
                            }
                        } else if (Objects.equals(filterConditionsPoJo.getValueFieldType(), FieldTypeEnum.DEPT.getType())) {
                            HashMap dept;
                            Long deptId ;
                            if (Objects.nonNull(optionalRangeDataList)){
                                dept = (HashMap) optionalRangeDataList.get(dynamicAttr);
                                if (Objects.nonNull(dept)){
                                    deptId = Long.valueOf(String.valueOf(dept.get("id")));
                                    if (conditionSymbol) {
                                        deptIdIn.add(deptId);
                                    } else{
                                        deptIdNoIn.add(deptId);
                                    }
                                }
                            }else {
                                if (Objects.nonNull(paasFormDataEntity.getData().get(dynamicAttr))){
                                    deptId = Long.valueOf(String.valueOf(paasFormDataEntity.getData().get(dynamicAttr)));
                                    if (conditionSymbol) {
                                        deptIdIn.add(deptId);
                                    } else{
                                        deptIdNoIn.add(deptId);
                                    }
                                }
                            }
                        }
                    }
                    break;
                case FIXED_VALUE:
                    List<ReturnUserAndDepartmentPojo> departmentList = JSON.parseArray(JSON.toJSONString(filterConditionsPoJo.getValue()), ReturnUserAndDepartmentPojo.class);
                    if (CollectionsUtil.isNotEmpty(departmentList)){
                        for (int i = 0; i < departmentList.size(); i++) {
                            ReturnUserAndDepartmentPojo userAndDepartmentPojo = departmentList.get(i);
                            Long id = Long.valueOf(String.valueOf(userAndDepartmentPojo.getId()));
                            if (conditionSymbol){
                                deptIdIn.add(id);
                            }else {
                                deptIdNoIn.add(id);
                            }

                        }
                    }
                    break;
                default:
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            deptMap.put("deptIdIn", deptIdIn);
            deptMap.put("deptIdNoIn", deptIdNoIn);
        } catch (XbbException e) {
            LOG.error("DeptNameHandlerImpl.filterDeptUserData 执行错误", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return deptMap;
    }
}
